﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls.Primitives;
using System.Windows.Controls;

namespace Kokomo
{
	public class Scrolling
	{
		public static double GetVerticalOffset(DependencyObject obj)
		{
			return (double)obj.GetValue(VerticalOffsetProperty);
		}

		public static void SetVerticalOffset(DependencyObject obj, double value)
		{
			obj.SetValue(VerticalOffsetProperty, value);
		}

		// Using a DependencyProperty as the backing store for VerticalOffset.  This enables animation, styling, binding, etc...
		public static readonly DependencyProperty VerticalOffsetProperty =
			DependencyProperty.RegisterAttached("VerticalOffset", typeof(double), typeof(Scrolling), new UIPropertyMetadata(0.0, OnVerticalOffsetChanged));

		private static void OnVerticalOffsetChanged(DependencyObject target, DependencyPropertyChangedEventArgs e)
		{
			IScrollInfo scroller = target as IScrollInfo;
			if (scroller != null)
			{
				scroller.SetVerticalOffset((double)e.NewValue);
			}
			else
			{
				ScrollViewer scrollViewer = target as ScrollViewer;
				if (scrollViewer != null)
				{
					scrollViewer.ScrollToVerticalOffset((double)e.NewValue);
				}
			}
		}



		public static double GetHorizontalOffset(DependencyObject obj)
		{
			return (double)obj.GetValue(HorizontalOffsetProperty);
		}

		public static void SetHorizontalOffset(DependencyObject obj, double value)
		{
			obj.SetValue(HorizontalOffsetProperty, value);
		}

		// Using a DependencyProperty as the backing store for HorizontalOffset.  This enables animation, styling, binding, etc...
		public static readonly DependencyProperty HorizontalOffsetProperty =
			DependencyProperty.RegisterAttached("HorizontalOffset", typeof(double), typeof(Scrolling), new UIPropertyMetadata(0.0, OnHorizontalOffsetChanged));


		private static void OnHorizontalOffsetChanged(DependencyObject target, DependencyPropertyChangedEventArgs e)
		{
			IScrollInfo scroller = target as IScrollInfo;
			if (scroller != null)
			{
				scroller.SetHorizontalOffset((double)e.NewValue);
			}
			else
			{
				ScrollViewer scrollViewer = target as ScrollViewer;
				if (scrollViewer != null)
				{
					scrollViewer.ScrollToHorizontalOffset((double)e.NewValue);
				}
			}
		}

	}
}
